﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GaussSolution;
using MatlabGaussSolution;

namespace GaussApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private IGaussSolution _solver;

        public MainWindow()
        {
            InitializeComponent();
        }

        private TextBox CreateMatrixCellView(int i, int j, string text, bool readOnly = false)
        {
            TextBox cell = new TextBox();
            cell.Text = text;
            cell.TextAlignment = TextAlignment.Center;
            cell.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            cell.IsReadOnly = readOnly;
            Grid.SetRow(cell, i);
            Grid.SetColumn(cell, j);
            cell.GotMouseCapture += new MouseEventHandler(cell_GotMouseCapture);
            return cell;
        }

        private void RefreshMatrixViews(int dimension)
        {
            aMatrixGrid.RowDefinitions.Clear();
            aMatrixGrid.ColumnDefinitions.Clear();
            aMatrixGrid.Children.Clear();
            bMatrixGrid.RowDefinitions.Clear();
            bMatrixGrid.Children.Clear();
            xMatrixGrid.RowDefinitions.Clear();
            xMatrixGrid.Children.Clear();
            for (int i = 0; i < dimension; i++)
                aMatrixGrid.ColumnDefinitions.Add(new ColumnDefinition());
            for (int i = 0; i < dimension; i++)
            {
                aMatrixGrid.RowDefinitions.Add(new RowDefinition());
                bMatrixGrid.RowDefinitions.Add(new RowDefinition());
                xMatrixGrid.RowDefinitions.Add(new RowDefinition());
            }
            for (int i = 0; i < dimension; i++)
            {
                TextBox cell = CreateMatrixCellView(i, 1, "0");
                bMatrixGrid.Children.Add(cell);
                cell = CreateMatrixCellView(i, 1, "?", true);
                xMatrixGrid.Children.Add(cell);
                for (int j = 0; j < dimension; j++)
                {
                    TextBox matrixCell = CreateMatrixCellView(i, j, "0");
                    aMatrixGrid.Children.Add(matrixCell);
                }
            }
        }

        void cell_GotMouseCapture(object sender, MouseEventArgs e)
        {
            TextBox cell = (TextBox)sender;
            cell.SelectAll();
        }

        private void dimensionsSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            int dimension = (int)dimensionsSlider.Value;
            dimensionsTextBox.Text = dimension.ToString();
            if (aMatrixGrid != null)
                RefreshMatrixViews(dimension);
          
            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshMatrixViews((int)dimensionsSlider.Value);
        }

        private void clearMatrixButton_Click(object sender, RoutedEventArgs e)
        {
            foreach (var child in aMatrixGrid.Children)
            {
                TextBox cell = child as TextBox;
                cell.Text = "0";
            }
            foreach (var child in bMatrixGrid.Children)
            {
                TextBox cell = child as TextBox;
                cell.Text = "0";
            }
            foreach (var child in xMatrixGrid.Children)
            {
                TextBox cell = child as TextBox;
                cell.Text = "?";
            }

        }

        private double[,] LoadMatrixA()
        {
            int dim = (int)dimensionsSlider.Value;
            double[,] aMatrix = new double[dim, dim];
            foreach (var child in aMatrixGrid.Children)
            {
                TextBox cell = child as TextBox;
                aMatrix[Grid.GetRow(cell), Grid.GetColumn(cell)] = double.Parse(cell.Text);
            }
            return aMatrix;
        }

        private double[] LoadMatrixB()
        {
            int dim = (int)dimensionsSlider.Value;
            double[] bMatrix = new double[dim];
            foreach (var child in bMatrixGrid.Children)
            {
                TextBox cell = (TextBox) child;
                bMatrix[Grid.GetRow(cell)] = double.Parse(cell.Text);
            }
            return bMatrix;
        }

        private void UpdateMatrixX(double[] values)
        {
            foreach (var child in xMatrixGrid.Children)
            {
                TextBox cell = child as TextBox;
                cell.Text = values[Grid.GetRow(cell)].ToString();
            }
        }

        private void computeButton_Click(object sender, RoutedEventArgs e)
        {
            if (matlabSelector.IsChecked == true)
                _solver = new MatlabGaussSolution.MatlabGaussSolution();
            UpdateMatrixX(_solver.Solve(LoadMatrixA(), LoadMatrixB()));
        }

    }
}
